﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;

namespace Ko2.Portal.SP_List
{
    public class SPList_errSPUsers
    {

        public Exception Insert(DateTime dt, string AccountName, string Login, string UserProfile_GUID, string ID1C, string HireDate, string ImageUrl, string SiteUrl)
        {
            Exception ret = null;
            SPUser spuser;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite("http://localhost/"))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList list = web.Lists["errSPUsers"];
                            SPListItem item = list.Items.Add();

                            item["Title"] = Login;
                            item["DT"] = dt;
                            spuser = null;
                            try
                            {
                                spuser = web.SiteUsers[AccountName];
                            }
                            catch
                            {
                            }                            
                            item["SPUser"] = spuser;
                            item["Login"] = Login;
                            item["UserProfile_GUID"] = UserProfile_GUID;
                            item["ID1C"] = ID1C;
                            item["HireDate"] = HireDate;
                            item["ImageUrl"] = ImageUrl;
                            item["SiteUrl"] = SiteUrl;

                            item.Update();
                        }
                    }
                });
            }
            catch (Exception err)
            {
                ret = new Exception(err.Message, err);
            }
            return ret;
        }

        public Exception DeleteAll()
        {
            Exception ret = null;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite("http://localhost/"))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList list = web.Lists["errSPUsers"];
                            int iCount = list.Items.Count;
                            for (int i = list.Items.Count - 1; i >= 0; i--)
                                list.Items[i].Delete();
                            list.Update();
                        }
                    }
                });
            }
            catch (Exception err)
            {
                ret = new Exception(err.Message, err);
            }
            return ret;
        }

    }
}
